package com.wandousoushu.jiusen.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.booksource.bean.BookBean;
import com.wandousoushu.jiusen.booksource.data.entities.Book;

import java.util.ArrayList;
import java.util.List;

public class BookShelfAdapter extends RecyclerView.Adapter<BookShelfAdapter.Vh> {

    private Context mContext;
    private List<Book> mBookList;
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private LongClickListener mLongClickListener;
    private View.OnClickListener mOnClickListener;
    private View.OnLongClickListener mOnLongClickListener;
    private CheckedListener mCheckedListener;
    private CheckBox.OnCheckedChangeListener mOnCheckedListener;
    private int[] bgBookShelf = {R.drawable.bg_bookshelf_item, R.drawable.bg_bookshelf_item1,
            R.drawable.bg_bookshelf_item2, R.drawable.bg_bookshelf_item3, R.drawable.bg_bookshelf_item4,
            R.drawable.bg_bookshelf_item5, R.drawable.bg_bookshelf_item6, R.drawable.bg_bookshelf_item7, R.drawable.bg_bookshelf_item8};
    SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    public boolean isStartSelect=false;
    private List<Book> mSelectList=new ArrayList<>();

    public BookShelfAdapter(Context mContext, List<Book> bookList) {
        this.mContext = mContext;
        this.mBookList = bookList;
        mInflater = LayoutInflater.from(mContext);
        this.notifyDataSetChanged();
        mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if (tag != null) {
                    int position = (int) tag;
                    if (mActionListener != null) {
                        mActionListener.onItemClick(mBookList.get(position));
                    }
                }
            }
        };
        mOnLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Object tag = view.getTag();
                if (tag != null) {
                    int position = (int) tag;
                    if (mLongClickListener != null) {
                        mLongClickListener.onItemLongClick(mBookList.get(position));
                    }
                }
                return false;
            }
        };
        mOnCheckedListener = new CheckBox.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
                Object tag = compoundButton.getTag();
                if (tag != null){
                    int position = (int) tag;
                    //setItemChecked(position,ischecked);
                    if(ischecked){
                        mSelectList.add(mBookList.get(position));
                    }else {
                        mSelectList.remove(mBookList.get(position));
                    }
                    if (mCheckedListener != null) {
                        mCheckedListener.onItemChecked(mSelectList);
                    }
                }
            }
        };
    }

    public void setActionListener(ActionListener listener) {
        mActionListener = listener;
    }

    public void setLongClickListener(LongClickListener listener) {
        mLongClickListener = listener;
    }

    public void setCheckedListener(CheckedListener listener) {
        mCheckedListener = listener;
    }

    @Override
    public BookShelfAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_bookshelf_layout, parent, false));
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    @Override
    public void onBindViewHolder(BookShelfAdapter.Vh holder, int position) {
        holder.setData(mBookList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mBookList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        CheckBox itemCheckBox;
        TextView originNameTextView;
        TextView nameBigTextView;
        TextView nameTextView;
        TextView originTextView;
        LinearLayout itemLinearLayout;

        public Vh(View itemView) {
            super(itemView);
            itemCheckBox=itemView.findViewById(R.id.bookshelf_cb_item);
            itemLinearLayout = itemView.findViewById(R.id.bookshelf_ll_item);
            originNameTextView = (TextView) itemView.findViewById(R.id.tv_bookshelf_origin_name);
            nameBigTextView = (TextView) itemView.findViewById(R.id.tv_bookshelf_name_big);
            nameTextView = (TextView) itemView.findViewById(R.id.tv_bookshelf_name);
            originTextView = (TextView) itemView.findViewById(R.id.tv_bookshelf_origin);
            itemView.setOnClickListener(mOnClickListener);
            itemView.setOnLongClickListener(mOnLongClickListener);
            itemCheckBox.setOnCheckedChangeListener(mOnCheckedListener);
        }

        void setData(Book book, int position) {
            itemView.setTag(position);
            itemCheckBox.setTag(position);
            itemLinearLayout.setBackgroundResource(bgBookShelf[position % 9]);
            originNameTextView.setText(book.getOriginName());
            nameBigTextView.setText(book.getName());
            nameTextView.setText(book.getName());
            originTextView.setText(book.getOrigin());
            if (isStartSelect){
                itemCheckBox.setVisibility(View.VISIBLE);
            }else {
                itemCheckBox.setVisibility(View.GONE);
            }
            itemCheckBox.setChecked(false);
            //mSelectedPositions.put(position,false);
        }
    }


    public void refresh(List<Book> newList) {
        mBookList.removeAll(mBookList);
        mBookList.addAll(newList);
        notifyDataSetChanged();
    }

    public boolean isStartSelect() {
        return isStartSelect;
    }

    public void setStartSelect(boolean startSelect) {
        isStartSelect = startSelect;
        mSelectList.clear();
        notifyDataSetChanged();
    }

    public List<Book> GetSelectList() {
        return mSelectList;
    }

    public interface ActionListener {
        void onItemClick(Book book);
    }

    public interface LongClickListener {
        void onItemLongClick(Book book);
    }

    public interface CheckedListener {
        void onItemChecked(List<Book> bookList);
    }
}
